<template>
  <div>
    <el-main>
      <el-form
        ref="searchForm"
        size="small"
        :model="searchModel"
        :inline="true"
        @keyup.enter.native="search"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="模板名称">
              <el-input
                placeholder="请输入模板名称"
                v-model="searchModel.name"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="searchModel.status" placeholder="请选择状态" clearable>
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="searchModel.createTime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始创建时间"
                end-placeholder="结束创建时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button icon="el-icon-refresh" size="small" @click="resetSearch"
            >重置
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="search"
            >搜索
            </el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <el-table
        ref="table"
        :data="tableData.records"
        :border="true"
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          :formatter="statusFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="sortIndex"
          label="排序"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          :formatter="datetimeFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-link type="primary" :underline="false" icon="el-icon-edit" @click="handleUpdate(row)">编辑</el-link>
            <el-link type="primary" :underline="false" icon="el-icon-delete" @click="handleDelete(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination
          v-show="tableData.total > 0"
          :total="tableData.total"
          :page.sync="tableData.current"
          :limit.sync="tableData.size"
          @pagination="search"
        />
      </div>
      <el-footer>
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click="handleInsert"
        >新增
        </el-button
        >
        <el-button type="primary"
                   icon="el-icon-unlock"
                   size="small"
                   @click="handleEnables"
        >启用
        </el-button
        >
        <el-button type="primary"
                   icon="el-icon-lock"
                   size="small"
                   @click="handleDisables"
        >禁用
        </el-button
        >
        <el-button type="primary"
                   icon="el-icon-delete"
                   size="small"
                   @click="handleDeletes"
        >删除
        </el-button
        >
      </el-footer>
    </el-main>
  </div>
</template>

<script>
  import Pagination from "@/components/Pagination";
  import {deleteTemplate,deletesTemplate,enablesTemplate,disablesTemplate,pageTemplate} from "@/api/cms/template";
  import {Status} from "@/utils/constant/status";
  import {parseTime} from "@/utils/common";
  import {ApiError} from "@/utils/errors";

  export default {
    components: {Pagination},
    data() {
      return {
        Status,
        searchAll: false,
        selection:[],
        searchModel:{
        },
        tableData: {
          records: [],
          total: 0,
          current: 1,
          size: 10
        }
      }
    },
    created() {
      this.search();
    },
    methods: {
      handleSelectionChange(selection){
        this.selection = selection;
      },
      handleInsert() {
        this.$router.push({path: "content-template-insert"});
      },
      handleUpdate(row) {
        this.$router.push({path: "content-template-update",query:{id:row.id}});
      },
      handleDelete(row) {
        this.$confirm(`是否删除模板${row.name} ?`, "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {
              id: row.id
            };
            deleteTemplate(params).then(res => {
              if (res.status == 200) {
                if (!res.data.success) {
                  return this.$message.error(ApiError.msg(res.data.message));
                }
                this.$message.success("删除模板成功");
                this.search();
              }
            });
          })
          .catch(() => {});
      },
      handleDeletes() {
        this.$confirm(`是否删除选中的${this.selection.length}个模板?`, "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {
              ids: this.selection
            };
            deletesTemplate(params).then(res => {
              if (res.status == 200) {
                if (!res.data.success) {
                  return this.$message.error(ApiError.msg(res.data.message));
                }
                this.$message.success(`成功删除${res.data.data.count}个模板`);
                this.search();
              }
            });
          })
          .catch(() => {});
      },
      handleEnables() {
        const params = {
          ids: this.selection
        };
        enablesTemplate(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            this.$message.success(`成功启用${res.data.data.count}个模板`);
            this.search();
          }
        });
      },
      handleDisables() {
        const params = {
          ids: this.selection
        };
        disablesTemplate(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            this.$message.success(`成功禁用${res.data.data.count}个模板`);
            this.search();
          }
        });
      },
      search() {
        const params = {
          name: this.searchModel.name,
          status: this.searchModel.status,
          current: this.tableData.current,
          size: this.tableData.size
        };
        if(this.searchModel.createTime){
          params.createTimeStart = this.searchModel.createTime[0];
          params.createTimeEnd = this.searchModel.createTime[1];
        }
        pageTemplate(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            const data = res.data.data;
            this.tableData.records = data.records;
            this.tableData.total = data.total;
            this.tableData.current = data.current;
            this.tableData.size = data.size;
          }
        });
      },
      resetSearch(){
        this.searchModel.name = null;
        this.searchModel.status = null;
        this.searchModel.createTime = null;
      },
      statusFormatter(row) {
        return Status[row.status]["name"];
      },
      datetimeFormatter(row) {
        return parseTime(row["createTime"]);
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "@/styles/full-form";
  @import "@/styles/common-table";
</style>
